package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

// handleFileUpload 处理普通文件上传
func handleFileUpload(w http.ResponseWriter, r *http.Request) error {
	// 解析表单
	err := r.ParseMultipartForm(32 << 20) // 32MB max memory
	if err != nil {
		return fmt.Errorf("failed to parse form: %v", err)
	}

	// 获取文件
	file, handler, err := r.FormFile("uploadfile")
	if err != nil {
		return fmt.Errorf("error retrieving file: %v", err)
	}
	defer file.Close()

	// 创建上传目录（如果不存在）
	uploadDir := "/data/servers/upload/file"
	if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
		err := os.MkdirAll(uploadDir, 0755)
		if err != nil {
			return fmt.Errorf("failed to create upload directory: %v", err)
		}
	}

	// 创建目标文件
	targetFile := filepath.Join(uploadDir, handler.Filename)
	dst, err := os.Create(targetFile)
	if err != nil {
		return fmt.Errorf("failed to create target file: %v", err)
	}
	defer dst.Close()

	// 复制文件内容
	_, err = io.Copy(dst, file)
	if err != nil {
		return fmt.Errorf("failed to save file: %v", err)
	}

	fmt.Printf("File uploaded successfully: %s\n", targetFile)
	return nil
}

// handleJarUpload 处理Jar文件上传
func handleJarUpload(w http.ResponseWriter, r *http.Request) error {
	// 解析表单
	err := r.ParseMultipartForm(100 << 20) // 100MB max memory
	if err != nil {
		return fmt.Errorf("failed to parse form: %v", err)
	}

	// 获取文件
	file, handler, err := r.FormFile("jarfile")
	if err != nil {
		return fmt.Errorf("error retrieving jar file: %v", err)
	}
	defer file.Close()

	// 创建上传目录（如果不存在）
	uploadDir := "/data/servers/upload/jarfile"
	if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
		err := os.MkdirAll(uploadDir, 0755)
		if err != nil {
			return fmt.Errorf("failed to create jar upload directory: %v", err)
		}
	}

	// 创建目标文件
	targetFile := filepath.Join(uploadDir, handler.Filename)
	dst, err := os.Create(targetFile)
	if err != nil {
		return fmt.Errorf("failed to create target jar file: %v", err)
	}
	defer dst.Close()

	// 复制文件内容
	_, err = io.Copy(dst, file)
	if err != nil {
		return fmt.Errorf("failed to save jar file: %v", err)
	}

	fmt.Printf("Jar file uploaded successfully: %s\n", targetFile)
	return nil
}

// handleFrontendUpload 处理前端文件上传
func handleFrontendUpload(w http.ResponseWriter, r *http.Request) error {
	// 解析表单
	err := r.ParseMultipartForm(100 << 20) // 100MB max memory
	if err != nil {
		return fmt.Errorf("failed to parse form: %v", err)
	}

	// 获取文件
	file, handler, err := r.FormFile("frontendfile")
	if err != nil {
		return fmt.Errorf("error retrieving frontend file: %v", err)
	}
	defer file.Close()

	// 创建上传目录（如果不存在）
	uploadDir := "/data/servers/upload/frontend"
	if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
		err := os.MkdirAll(uploadDir, 0755)
		if err != nil {
			return fmt.Errorf("failed to create frontend upload directory: %v", err)
		}
	}

	// 创建目标文件
	targetFile := filepath.Join(uploadDir, handler.Filename)
	dst, err := os.Create(targetFile)
	if err != nil {
		return fmt.Errorf("failed to create target frontend file: %v", err)
	}
	defer dst.Close()

	// 复制文件内容
	_, err = io.Copy(dst, file)
	if err != nil {
		return fmt.Errorf("failed to save frontend file: %v", err)
	}

	fmt.Printf("Frontend file uploaded successfully: %s\n", targetFile)
	return nil
}
